﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Kkc.Application.Specifications
{
    public abstract class Specification<T>: ISpecification<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Specification&lt;T&gt;"/> class.
        /// </summary>
        protected Specification()
        {
        }

        private Func<T, bool> compiledExpression;

        /// <summary>
        /// Determines whether the <see cref="Specification&lt;T&gt;"/> is satisfied by the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>
        /// 	<c>true</c> if the <see cref="Specification&lt;T&gt;"/> is satisfied by the specified entity; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSatisfiedBy(T entity)
        {
            if (compiledExpression == null)
                compiledExpression = Expression.Compile();

            return compiledExpression(entity);
        }

        /// <summary>
        /// Gets the expression defining the <see cref="Specification&lt;T&gt;"/>.
        /// </summary>
        /// <value>The expression.</value>
        public abstract System.Linq.Expressions.Expression<Func<T, bool>> Expression
        {
            get;
        }
    }
}